﻿using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Data;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System.Text;
using CommDll;
using System.Xml.Linq;
using System.Xml;
using System.Drawing;
using System.ComponentModel;
using DevComponents.DotNetBar.Controls;
namespace InstitutionAssitant
{
    /// <summary>
    /// ClassName:frmQuery_MedicineCodeCheck0
    /// Version:1.0
    /// Date:2019-06-23
    /// Author:seanouyang
    /// </summary>
    /// <remarks>
    /// 本类是一WinForm 窗体,本窗体主要为实现企业对码(先确定平台药品信息)
    /// </remarks>
    public partial class frmQuery_MedicineCodeCheck0 : DevComponents.DotNetBar.OfficeForm
    {
        #region 01 自定义属性
        public Dictionary<string, string> m_dictInfo = new Dictionary<string, string>(); // 回传信息(指定对码时用)
        public DataGridViewRow m_dgvRow = new DataGridViewRow();
        #endregion

        public frmQuery_MedicineCodeCheck0()
        {
            InitializeComponent();
        }

        #region 003 自定义方法
        /// <summary>
        /// 初始化DataGridView字段显示信息
        /// </summary>
        public void _initDgvTitle()
        {
            string strInfo = "";
            string strKind = "";

            strKind = this.Name + "1";
            // 存在时不再处理
            if (AppConfig.m_dictFieldName.ContainsKey(strKind))
            {
                return;
            }
            strInfo = "ID-序号,Code-药品编码,Name-药品名称,ApprovalNumber-批准文号,Form-剂型,PackageSpec-规格,WarehouseUnit-单位,PackageRatio-包装比,Manufacturer-生产厂家,Price-进价";
            AppConfig.m_dictFieldName.Add(strKind, strInfo);
            strInfo = "ID,Code,Name,ApprovalNumber,Form,PackageSpec,WarehouseUnit,PackageRatio,Manufacturer,Price";
            AppConfig.m_dictFieldSql.Add(strKind, strInfo);
            strInfo = "ID,Code,Name,ApprovalNumber,Form,PackageSpec,WarehouseUnit,PackageRatio,Manufacturer,Price";
            AppConfig.m_dictField0.Add(strKind, strInfo);
            strInfo = "0,120,200,150,80,120,60,100,200,80";
            AppConfig.m_dictFieldWidth0.Add(strKind, strInfo);
            // 设置值
            strInfo = AppConfig.m_iniSetup._readValue("Common", string.Format("{0}_Field", strKind), AppConfig.m_dictField0[strKind]).Trim();
            AppConfig.m_dictField.Add(strKind, strInfo);
            strInfo = AppConfig.m_iniSetup._readValue("Common", string.Format("{0}_FieldWidth", strKind), AppConfig.m_dictFieldWidth0[strKind]).Trim();
            AppConfig.m_dictFieldWidth.Add(strKind, strInfo);
        }

        /// <summary>
        /// 查询HIS药品信息
        /// </summary>
        private void _queryGoods1()
        {
            try
            {
                // 01 条件
                string strWhere = "", strTemp = "";
                string strTemp1 = txtWhere1.Text.Trim();
                if (!strTemp1.Equals("") && (cbWhere11.Checked || cbWhere12.Checked || cbWhere13.Checked || cbWhere14.Checked))
                {
                    if (cbWhere11.Checked)
                    {
                        strTemp += (strTemp.Equals("") ? "" : " or ") + string.Format("Code like '%{0}%' ", strTemp1);
                    }
                    if (cbWhere12.Checked)
                    {
                        strTemp += (strTemp.Equals("") ? "" : " or ") + string.Format("Name like '%{0}%' ", strTemp1);
                    }
                    if (cbWhere13.Checked)
                    {
                        strTemp += (strTemp.Equals("") ? "" : " or ") + string.Format("RegistrationCode like '%{0}%' ", strTemp1);
                    }
                    if (cbWhere14.Checked)
                    {
                        strTemp += (strTemp.Equals("") ? "" : " or ") + string.Format("Manufacturer like '%{0}%' ", strTemp1);
                    }
                    strWhere = string.Format(" where isnull(status,1)=1 and ({0})", strTemp);
                }
                //else
                //{
                //    CommFun._messageBoxShow("查询条件不能为空!");
                //    return;
                //}

                // 03 取数据
                string strSql = string.Format("SELECT {0} FROM TH_Goods {{0}}  order by ID", AppConfig.m_dictFieldSql[this.Name + "1"]);
                strSql = string.Format(strSql, strWhere);
                DataTable dt = DbHelper.DBAccess[0]._openSql_Dt(strSql);

                if (dt != null && dt.Rows.Count > 0)
                {
                    // 04 显示数据
                    // 0401 调整字段顺序
                    CommFun._dataTableFieldMove(dt, AppConfig.m_dictField[this.Name + "1"]);
                    // 0402 显示数据
                    CommFun._showDataGridView0(dgv1, dt, AppConfig.m_dictFieldWidth[this.Name + "1"], AppConfig.m_dictFieldName[this.Name + "1"]);
                }
                else
                {
                    // 清除数据
                    DataTable dtTemp = (DataTable)dgv1.DataSource;
                    if (dtTemp != null && dtTemp.Rows.Count > 0)
                    {
                        dtTemp.Rows.Clear();
                    }
                }
            }
            catch (Exception ex)
            {
                CommFun._messageBoxShow(ex.Message);
                return;
            }
        }

        /// <summary>
        /// 药品对码
        /// </summary>
        private void _checkMedicineCode()
        {
            try
            {
                frmGoodsCodeCheck frm = new frmGoodsCodeCheck();
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    DialogResult = DialogResult.OK;
                }
            }
            catch (Exception ex)
            {
                CommFun._messageBoxShow(ex.Message);
                return;
            }
        }
        #endregion

        #region  窗体相关事件方法

        private void btnQuery1_Click(object sender, EventArgs e)
        {
            _queryGoods1();
        }

        private void txtWhere1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnQuery1_Click(null, null);
            }
        }
        #endregion

        #region  DataGridView 事件方法    

        private void frmQuery_MedicineCodeCheck_Load(object sender, EventArgs e)
        {
            timerInit.Enabled = true;
            dgv1.Anchor = (System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Top));
        }
        private void timerInit_Tick(object sender, EventArgs e)
        {
            timerInit.Enabled = false;
            // 初始化DataGridView
            _initDgvTitle();

            // 初始化控件
            string strSql = string.Format("select * from T_Base_GoodsCodeCheck where SupplierCode = '{0}' and CodeSupplier = '{1}'", m_dictInfo["SupplierCode"], m_dictInfo["CodeSupplier"]);
            DataRow dr = DbHelper.DBAccess[0]._openSql_Row(strSql);
            if (dr != null)
            {
                ModelGoodsCodeCheck.strID0 = dr["ID"].ToString();
                ModelGoodsCodeCheck.strSupplierName = dr["SupplierName"].ToString();
                ModelGoodsCodeCheck.strSupplierCode = dr["SupplierCode"].ToString();
                ModelGoodsCodeCheck.strCode1 = dr["Code"].ToString();
                ModelGoodsCodeCheck.strCode0 = dr["CodeSupplier"].ToString();
                ModelGoodsCodeCheck.strName0 = dr["Name"].ToString();
                ModelGoodsCodeCheck.strName1 = m_dgvRow.Cells["Name_His"].Value.ToString();
                ModelGoodsCodeCheck.strRegistrationCode0 = dr["RegistrationCode"].ToString();
                ModelGoodsCodeCheck.strManufacturer0 = dr["Manufacturer"].ToString();
                ModelGoodsCodeCheck.strSpec0 = dr["Spec"].ToString();
                ModelGoodsCodeCheck.strUnit0 = dr["Unit"].ToString();
                ModelGoodsCodeCheck.strUnitMinForm0 = dr["UnitMinForm"].ToString();
                ModelGoodsCodeCheck.strForm0 = dr["Form"].ToString();
                ModelGoodsCodeCheck.dWarehouseRatio0 = FunctionComm._strToFloat(dr["WarehouseRatio"].ToString(), 1);
                ModelGoodsCodeCheck.strWarehouseCode0 = dr["WarehouseCode"].ToString();
                ModelGoodsCodeCheck.strWarehouseName0 = dr["WarehouseName"].ToString();
                ModelGoodsCodeCheck.strKind0 = dr["Kind"].ToString();
            }
            else
            {
                // 取目前最新值
                ModelGoodsCodeCheck.strID0 = "0";
                ModelGoodsCodeCheck.strSupplierName = m_dgvRow.Cells["SupplierName"].Value.ToString();
                ModelGoodsCodeCheck.strSupplierCode = m_dgvRow.Cells["SupplierCode"].Value.ToString();
                ModelGoodsCodeCheck.strCode0 = m_dgvRow.Cells["Code"].Value.ToString();
                ModelGoodsCodeCheck.strCode1 = m_dgvRow.Cells["Code_His"].Value.ToString();
                ModelGoodsCodeCheck.strName0 = m_dgvRow.Cells["Name"].Value.ToString();
                ModelGoodsCodeCheck.strName1= m_dgvRow.Cells["Name_His"].Value.ToString();
                ModelGoodsCodeCheck.strRegistrationCode0 = m_dgvRow.Cells["RegistrationCode"].Value.ToString();
                ModelGoodsCodeCheck.strManufacturer0 = m_dgvRow.Cells["Manufacturer"].Value.ToString();
                ModelGoodsCodeCheck.strSpec0 = m_dgvRow.Cells["Spec"].Value.ToString();
                ModelGoodsCodeCheck.strUnit0 = m_dgvRow.Cells["Unit"].Value.ToString();
                ModelGoodsCodeCheck.strUnitMinForm0 = m_dgvRow.Cells["Unit"].Value.ToString();
                ModelGoodsCodeCheck.strForm0 = m_dgvRow.Cells["Form"].Value.ToString();
                ModelGoodsCodeCheck.dWarehouseRatio0 = 1;
                ModelGoodsCodeCheck.strWarehouseCode0 = "";
                ModelGoodsCodeCheck.strWarehouseName0 = "";
                ModelGoodsCodeCheck.strKind0 = "";
            }
            ModelGoodsCodeCheck.strPrice0 = string.Format("{0:0.00}", m_dgvRow.Cells["Price"].Value);
            txtSupplierName.Text = ModelGoodsCodeCheck.strSupplierName;
            txtCode0.Text = ModelGoodsCodeCheck.strCode1;
            txtUnit0.Text = ModelGoodsCodeCheck.strUnit0;
            txtUnitRatio.Text = ModelGoodsCodeCheck.dWarehouseRatio0.ToString();
            txtName0.Text = ModelGoodsCodeCheck.strName0;
            txtForm0.Text = ModelGoodsCodeCheck.strForm0;
            txtApproveCode0.Text = ModelGoodsCodeCheck.strRegistrationCode0;
            txtSpec0.Text = ModelGoodsCodeCheck.strSpec0;
            txtMaker0.Text = ModelGoodsCodeCheck.strManufacturer0;
            txtPrice0.Text = ModelGoodsCodeCheck.strPrice0;

            if (!txtCode0.Text.Trim().Equals(""))
            {
                txtCode0_Click(txtCode0, null);
            }
            else
            {
                txtCode0_Click(txtName0, null);
            }
        }
        #endregion

        #region  DataGridView 事件方法
        private void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            CommFun._dgvColumnHeaderSort(sender, e);
        }

        private void dataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            // 对码
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                return;
            }
            // 初始化数据
            DataGridView dgv = (DataGridView)sender;
            ModelGoodsCodeCheck.strCode1 = dgv.Rows[e.RowIndex].Cells["Code"].Value.ToString();
            ModelGoodsCodeCheck.strUnit1 = dgv.Rows[e.RowIndex].Cells["WarehouseUnit"].Value.ToString();
            ModelGoodsCodeCheck.strName1 = dgv.Rows[e.RowIndex].Cells["Name"].Value.ToString();
            ModelGoodsCodeCheck.strForm1 = dgv.Rows[e.RowIndex].Cells["Form"].Value.ToString();
            ModelGoodsCodeCheck.strRegistrationCode1 = dgv.Rows[e.RowIndex].Cells["ApprovalNumber"].Value.ToString();
            ModelGoodsCodeCheck.strSpec1 = dgv.Rows[e.RowIndex].Cells["PackageSpec"].Value.ToString();
            ModelGoodsCodeCheck.strManufacturer1 = dgv.Rows[e.RowIndex].Cells["Manufacturer"].Value.ToString();
            //ModelGoodsCodeCheck.strWarehouseCode1 = dgv.Rows[e.RowIndex].Cells["WarehouseCode"].Value.ToString();
            //ModelGoodsCodeCheck.strWarehouseName1 = dgv.Rows[e.RowIndex].Cells["WarehouseName"].Value.ToString(); 
            ModelGoodsCodeCheck.dPrice1 = FunctionComm._strToFloat(dgv.Rows[e.RowIndex].Cells["Price"].Value.ToString());
            // 对码
            _checkMedicineCode();
        }

        private void dataGridViewX1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            // 改变行颜色
            CommFun._dgvRowEnter(sender, e);
        }

        private void dataGridView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            // 设置标题信息    
            if (e.Button == MouseButtons.Right)
            {
                DataGridView dgv = (DataGridView)sender;
                frmDataShowSetup frm = new frmDataShowSetup();
                switch (dgv.Name)
                {
                    case "dgv1":
                        frm.m_strInfoKind = this.Name + "1";
                        if (frm.ShowDialog() == DialogResult.OK)
                        {
                            if (frm.m_bIsSaveWidth)
                            {
                                // 保存宽度
                                AppConfig._saveDgvWidth(dgv1, frm.m_strInfoKind);
                                CommFun._messageBoxShow("显示宽度保存成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            else
                            {
                                if (dgv1.Rows.Count > 0)
                                {
                                    btnQuery1_Click(null, null);
                                }
                            }
                        }
                        break;
                }
            }
        }
        #endregion 

        private void txtCode0_Click(object sender, EventArgs e)
        {
            // 快速查找数据
            TextBox tb = (TextBox)sender;
            cbWhere11.Checked = tb.Name.Equals("txtCode0") ? true : false;
            cbWhere12.Checked = tb.Name.Equals("txtName0") ? true : false;
            cbWhere13.Checked = tb.Name.Equals("txtApproveCode0") ? true : false;
            cbWhere14.Checked = tb.Name.Equals("txtMaker0") ? true : false;
            txtWhere1.Text = tb.Text;
            btnQuery1_Click(null, null);
        }

    }
}
